import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/join_family_request.dart';
import 'package:elephant_fish/model/family_entity.dart';
import 'package:elephant_fish/util/Alert.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:elephant_fish/util/size_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class FamilyList extends StatefulWidget {
  final FamilyEntity familyEntity;
  const FamilyList({Key key, this.familyEntity}) : super(key: key);

  @override
  _FamilyListState createState() => _FamilyListState();
}

class _FamilyListState extends State<FamilyList> {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Align(alignment: Alignment.topLeft,
          child: Padding(
            padding: EdgeInsets.only(top: SizeUtil.px(37),left: SizeUtil.px(74)),
            child:CircleAvatar(
              radius: SizeUtil.px(35),
              backgroundImage: NetworkImage(widget.familyEntity.icon == ''? 'https://img2.baidu.com/it/u=619635312,2155741865&fm=26&fmt=auto&gp=0.jpg' :'http://119.28.131.94:82' + widget.familyEntity.icon),
              child: Container(
                width: SizeUtil.px(70),
                height: SizeUtil.px(70),
              ),
            ),
          ),
        ),
        Align(alignment: Alignment.topLeft,
          child: Padding(
              padding: EdgeInsets.only(top: SizeUtil.px(39),left: SizeUtil.px(172)),
              child:Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(widget.familyEntity.name,style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),),
                  SizedBox(height: 6,),
                  Row(children: [
                    Container(
                      width: 60,
                      height: 14,
                      color: Color(0xff00ccf9),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(Icons.person,size: 10,color: Color(0xffffffff),),
                          SizedBox(width: 3.5,),
                          Text(widget.familyEntity.persons.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 12),)
                        ],
                      ),
                    ),
                    SizedBox(width: 6,),
                    Container(
                      width: 84,
                      height: 14,
                      color: Color(0xffc972ff),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text('ID：' + widget.familyEntity.id.toString(),style: TextStyle(color: Color(0xfff3f3f3),fontSize: 10),)
                        ],
                      ),
                    )
                  ],),
                  SizedBox(height: 6,),
                   Text( widget.familyEntity.introduction,style: TextStyle(fontSize: SizeUtil.px(22),color: Color(0x68f3f3f3)),),


                ],
              )
          ),
        ),
        Align(alignment: Alignment.centerRight,
          child: Padding(
              padding: EdgeInsets.only(top: SizeUtil.px(56),right: SizeUtil.px(73)),
              child: InkWell(
                child: Container(
                    width: SizeUtil.px(132),
                    height: SizeUtil.px(56),
                    decoration: BoxDecoration(
                        color: Color(0xffe62d6c),
                        borderRadius: BorderRadius.all(Radius.circular(SizeUtil.px(43)))
                    ),
                    child:  Center(
                      child: Text('加入',style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),),
                    )
                ),
                onTap: (){
                  showJoinDialog();
                },
              )
          ),
        ),
        Align(
          alignment: Alignment.topLeft,
          child: Padding(
            padding: EdgeInsets.only(top: SizeUtil.px(160)),
            child: Divider(
              height: SizeUtil.px(1),
              color: Color(0xfff3f3f3),
            ),
          ),
        ),
      ],
    );
  }

  Future<void> showJoinDialog() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    Alert.showCustomDialog(
        context: context,
        title: '家族提示',
        message: '您确定要加入'+ widget.familyEntity.name +'家族吗',
        certainPressed: () async {
          BaseRequest request = JoinFamilyRequest();
          request.add('id', widget.familyEntity.id).addHeader('usertoken', appToken);
          var result = await HiNet.getInstance().fire(request);
          if (result['code'] == 0) {
            TxUtils.showToast('申请成功', context);

          } else {
            TxUtils.showToast(result['message'], context);
          }
        });
  }
}
